<?php

namespace App\Plugins;

use App\Models\Company;
use Illuminate\Support\Facades\Auth;

class PluginInfo
{
    private $plugin;
    private $namespace;
    private static $_instance;
    const PLUGIN_NAME = ['shh' => 'shihui'];

    private function __construct()
    {
        //$user = Auth::User();
        $request = request();
        $user = request()->user();
//        dd($user);
        $company = Company::find($user->company_id);
        if (is_null($company)) {
            $this->plugin = false;
        } else {
            $this->plugin = array_get($company, 'options.plugins', false);
            if ($this->plugin) {
                $this->namespace = 'App\\Plugins\\' . 'Shh';
            }
        }
    }

    public static function getInstance() {
        if (self::$_instance) {
            return self::$_instance;
        }
        return new self();
    }

    public function getPlugin() {
        return $this->plugin;
    }

    public function getNamespace() {
        return $this->namespace;
    }

    public function getName() {
        if ($this->plugin) {
            return self::PLUGIN_NAME['shh'];
        }
        return '';
    }

    public function registerRoute() {
        if ($this->plugin) {
            $web_route_name = $this->namespace . '\\Route\\Web';
            $app_route_name = $this->namespace . '\\Route\\App';
            (new $web_route_name())->register();
            (new $app_route_name())->register();
        }
    }
}
